Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### Coding The Wines Form The code for the Wines form is a bit more complicated. As for handling the Command Buttons, follow the exact same procedure as you did for the Customers form, including the use of chameleon Command Buttons. The code for this event procedure is shown in Listing 22.3. Listing 22.3 The Command1_Click event procedure in WINES.FRM.
    Private Sub Command1_Click(Index As Integer)

    Dim Reply As Integer

    Select Case Index
        Case 0      ‘ Add New or Save.
            If EnteringRecord Then      ‘ Save command.
                ‘ Save the new record.
                Adodc1.Recordset.Update
                EnteringRecord = False
                ‘ Relock the StockNo TextBox.
                Text1(0).Locked = True
                ‘ Change command buttons.
                Command1(0).Caption = “&Add New”
                Command1(1).Caption = “&Delete Current”
                ‘ Enable the ADO Data control.
                Adodc1.Enabled = True
                Adodc1.Recordset.Requery
                Adodc1.Recordset.MoveLast
                ‘ Enable List and Finished buttons.
                Command1(2).Enabled = True
                Command1(3).Enabled = True
            Else                        ‘ Add New command.
                EnteringRecord = True
                ‘ Unlock the StockNo Text Box.
                Text1(0).Locked = False
               ‘ Put focus in first text box.
                Text1(0).SetFocus
                ‘ Disable List and Finished buttons.
                Command1(2).Enabled = False
                Command1(3).Enabled = False
                ‘ Change command buttons.
                Command1(0).Caption = “&Save”
                Command1(1).Caption = “&Cancel”
                ‘ Disable the data control.
                Adodc1.Enabled = False
                ‘ Add a new record.
                Adodc1.Refresh
                Adodc1.Recordset.AddNew
            End If
        Case 1      ‘ Delete current or Cancel.
            If EnteringRecord Then      ‘ Cancel command.
                Reply = MsgBox(“Discard current entry?”, vbYesNo + vbQuestion, _   “Cancel Entry”)
                If Reply = vbNo Then Exit Sub
                Adodc1.Recordset.CancelUpdate
                Adodc1.Refresh
                Adodc1.Enabled = True
                ‘ Relock the StockNo TextBox.
                Text1(0).Locked = True
                EnteringRecord = False
                ‘ Enable List and Finished buttons.
                Command1(2).Enabled = True
                Command1(3).Enabled = True
                Command1(0).Caption = “&Add New”
                Command1(1).Caption = “&Delete Current”
            Else                        ‘ Delete Current command.
                Reply = MsgBox(“Delete this record?”, vbYesNo + vbQuestion, _      “Cancel Entry”)
                If Reply = vbNo Then Exit Sub
                Adodc1.Recordset.Delete
                Adodc1.Recordset.MoveNext
            End If
        Case 2      ‘ List all.
            frmList.Caption = “Wines Table”
            frmList.Adodc1.ConnectionString = frmWines.Adodc1.ConnectionString
            frmList.Adodc1.RecordSource = frmWines.Adodc1.RecordSource
            frmList.Adodc1.Refresh
            frmList.Show
        Case 3      ‘ Finished.
            Hide
    End Select

    End Sub
The Wines form is different, because the Combo Box controls that are used for entry of the Color and Type data must be loaded with the appropriate choices, in order for those choices to be available when the user displays the form. Place this code in the Form_Load event procedure, as shown in Listing 22.4. Another method is to load each Combo Box from a text file that is kept in the same directory as the database, or from a dedicated table within the database itself. In fact, this method is preferable for a commercial program, because this method permits changes to the Color and Type lists without modifying the source code and recompiling the program—editing the text file is all that is needed. Listing 22.4 The Form_Load event procedure in WINES.FRM.
    Private Sub Form_Load()

    ‘ Load the Combo boxes.

    ComboColor.AddItem “red”
    ComboColor.AddItem “white”
    ComboColor.AddItem “rose”
    ComboColor.AddItem “dessert”
    ComboColor.AddItem “sparkling”

    ComboType.AddItem “Chardonnay”
    ComboType.AddItem “Pinot noir”
    ComboType.AddItem “Burgundy”
    ComboType.AddItem “Pinot blanc”
    ComboType.AddItem “Barolo”
    ComboType.AddItem “Barbaresco”
    ComboType.AddItem “Cabernet”
    ComboType.AddItem “Semillion”
    ComboType.AddItem “Bordeaux”
    ComboType.AddItem “Chateauneuf de Pape”
    ComboType.AddItem “White zinfandel”
    ComboType.AddItem “Champagne”
    Adodc1.Refresh

    End Sub
#### Coding The List Form Coding the List form is very simple, because most of the form’s work is already handled by the DataGrid control—all that functionality is built right in, so you don’t have to do a thing. (Aren’t software components great?) However, you do need to add a little code. In the Form_Resize event procedure, you need to adjust the control sizes; this code is shown in Listing 22.5. Note that you are concerned only with the height of the DataGrid control; when you set its Align property to vbAlignTop, it is automatically positioned at the top of the form and is sized to match the form’s width. Listing 22.5 The Form_Resize event procedure in LIST1.FRM.
    Private Sub Form_Resize()

    ‘ Set size and position of controls.

    DataGrid1.Height = ScaleHeight - Command1.Height
    Command1.Top = ScaleHeight - Command1.Height
    Command1.Left = 0
    Command1.Width = ScaleWidth

    End Sub
The only other code required for the List form is to place the single line of code
    Hide
in the Command Button’s Click event procedure. By hiding the List form, you automatically return to the form that called it—the Customers form or the Wines form—which had remained inactive in the background while the List form was displayed. ### What Now? The answer to that one is easy: Try it out. Now you have a partially functional database front end to take on a trial run. Watch for error messages; if any appear, check your code to be sure that you entered everything properly. Currently, the program gives you the capability to add new records to the Customers and Wines tables, edit and delete existing records, and view a list of all records in either table. Note that changes to the table data made in List view are saved, providing another way for the user to edit table data. You’re off to a good start. But clearly, you have a long road ahead before you have a fully functional program.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.